/*******************************************************************************
 * Copyright (c) 2011 TXT e-solutions SpA
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * This work was performed within the IoT_at_Work Project
 * and partially funded by the European Commission's
 * 7th Framework Programme under the research area ICT-2009.1.3
 * Internet of Things and enterprise environments.
 *
 *
 * Authors:
 *     Cristoforo Seccia (TXT e-solutions SpA)
 *
 * Contributors:
 *      Domenico Rotondi (TXT e-solutions SpA)
 *******************************************************************************/
package it.txt.access.capability.wizard.panel;

import it.txt.access.capability.commons.filter.FilterXML;
import it.txt.access.capability.wizard.panel.controller.ControllerPanelAccessType;
import java.awt.Frame;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 *
 * @author Cristoforo Seccia (TXT e-solutions SpA)
 */
public class PanelAccessType extends javax.swing.JPanel {

    private ControllerPanelAccessType controller;
    private File currentCapabilityDir = null;
    private JFileChooser delegationChooser;
    private FilterXML filterAssertion;
    private Frame myContainer;
    
    /** Creates new form WelcomePanel */
    public PanelAccessType(Frame container, ControllerPanelAccessType cpat) {
        initComponents();
        checkDelegationPathDocument();
        controller = cpat;
        myContainer = container;
        delegationChooser = new JFileChooser();
        filterAssertion = new FilterXML("Access Rights Capability");
        delegationChooser.setFileFilter(filterAssertion);
        delegationChooser.setMultiSelectionEnabled(false);
        accessPolicyRadio.setName(ControllerPanelAccessType.ACCESS_POLICY_TYPE);
        accessCapabilityRadio.setName(ControllerPanelAccessType.ACCESS_CAPABILITY_TYPE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        centerPanel = new javax.swing.JPanel();
        centerTitlePanel = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        welcomeLabel = new javax.swing.JLabel();
        accessPolicyRadio = new javax.swing.JRadioButton();
        accessCapabilityRadio = new javax.swing.JRadioButton();
        delegationPanel = new javax.swing.JPanel();
        delegationLabel = new javax.swing.JLabel();
        delegationText = new javax.swing.JTextField();
        policyAccessRightsCapabilityIDButton = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        delegatioNoteLabel = new javax.swing.JLabel();
        jSeparator3 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        delegationNotesArea = new javax.swing.JTextArea();

        centerPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        centerTitlePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("IoT@Work - Capability Based Authorization"));

        welcomeLabel.setText("Please choose the Capability Type");

        buttonGroup1.add(accessPolicyRadio);
        accessPolicyRadio.setSelected(true);
        accessPolicyRadio.setText("Root Access Capability");
        accessPolicyRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                accessPolicyRadioActionPerformed(evt);
            }
        });

        buttonGroup1.add(accessCapabilityRadio);
        accessCapabilityRadio.setText("Access Capability");
        accessCapabilityRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                accessCapabilityRadioActionPerformed(evt);
            }
        });

        delegationPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Delegation"));

        delegationLabel.setText("Delegation path *");

        delegationText.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));

        policyAccessRightsCapabilityIDButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icons_16x16/Open_16x16.png"))); // NOI18N
        policyAccessRightsCapabilityIDButton.setText("Attach");
        policyAccessRightsCapabilityIDButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                policyAccessRightsCapabilityIDButtonActionPerformed(evt);
            }
        });

        delegatioNoteLabel.setText("Notes");

        delegationNotesArea.setColumns(20);
        delegationNotesArea.setEditable(false);
        delegationNotesArea.setFont(new java.awt.Font("Monospaced", 1, 14));
        delegationNotesArea.setRows(5);
        jScrollPane1.setViewportView(delegationNotesArea);

        javax.swing.GroupLayout delegationPanelLayout = new javax.swing.GroupLayout(delegationPanel);
        delegationPanel.setLayout(delegationPanelLayout);
        delegationPanelLayout.setHorizontalGroup(
            delegationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, delegationPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(delegationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)
                    .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)
                    .addGroup(delegationPanelLayout.createSequentialGroup()
                        .addComponent(delegationLabel)
                        .addGap(18, 18, 18)
                        .addComponent(delegationText, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(policyAccessRightsCapabilityIDButton))
                    .addComponent(jSeparator3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)
                    .addComponent(delegatioNoteLabel, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        delegationPanelLayout.setVerticalGroup(
            delegationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(delegationPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(delegationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(delegationLabel)
                    .addComponent(delegationText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(policyAccessRightsCapabilityIDButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(delegatioNoteLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout centerTitlePanelLayout = new javax.swing.GroupLayout(centerTitlePanel);
        centerTitlePanel.setLayout(centerTitlePanelLayout);
        centerTitlePanelLayout.setHorizontalGroup(
            centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, centerTitlePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(delegationPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(accessCapabilityRadio, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(accessPolicyRadio, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(welcomeLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE))
                .addContainerGap())
        );
        centerTitlePanelLayout.setVerticalGroup(
            centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(centerTitlePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(welcomeLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(accessPolicyRadio)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(accessCapabilityRadio)
                .addGap(18, 18, 18)
                .addComponent(delegationPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout centerPanelLayout = new javax.swing.GroupLayout(centerPanel);
        centerPanel.setLayout(centerPanelLayout);
        centerPanelLayout.setHorizontalGroup(
            centerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(centerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(centerTitlePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        centerPanelLayout.setVerticalGroup(
            centerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(centerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(centerTitlePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(centerPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(centerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void accessPolicyRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_accessPolicyRadioActionPerformed
        // TODO add your handling code here:
        validatePanelStatus();
    }//GEN-LAST:event_accessPolicyRadioActionPerformed

    private void accessCapabilityRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_accessCapabilityRadioActionPerformed
        // TODO add your handling code here:
        validatePanelStatus();
    }//GEN-LAST:event_accessCapabilityRadioActionPerformed

    private void policyAccessRightsCapabilityIDButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_policyAccessRightsCapabilityIDButtonActionPerformed
        handlePolicyAccessRightsCapabilityIDButtonActionPerformed();
    }//GEN-LAST:event_policyAccessRightsCapabilityIDButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton accessCapabilityRadio;
    private javax.swing.JRadioButton accessPolicyRadio;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JPanel centerPanel;
    private javax.swing.JPanel centerTitlePanel;
    private javax.swing.JLabel delegatioNoteLabel;
    private javax.swing.JLabel delegationLabel;
    private javax.swing.JTextArea delegationNotesArea;
    private javax.swing.JPanel delegationPanel;
    private javax.swing.JTextField delegationText;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JButton policyAccessRightsCapabilityIDButton;
    private javax.swing.JLabel welcomeLabel;
    // End of variables declaration//GEN-END:variables

    private void handlePolicyAccessRightsCapabilityIDButtonActionPerformed() {
        if (currentCapabilityDir == null) {
            delegationChooser.setCurrentDirectory(new File("."));
        }
        else{
            delegationChooser.setCurrentDirectory(currentCapabilityDir);
        }
        int returnValue = delegationChooser.showOpenDialog(myContainer);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File assertionFile = delegationChooser.getSelectedFile();
            if(controller.recoverAccessRightsCapability(assertionFile)){
                delegationNotesArea.setText(controller.getAccessRightsPublicValuesToString());
            }
            else{
                delegationNotesArea.setText(controller.recoverAccessRightsCapabilityErrorMessage());
            }
            delegationText.setText(assertionFile.getAbsolutePath());
            currentCapabilityDir = delegationChooser.getSelectedFile().getParentFile();
            delegationNotesArea.setCaretPosition(0);
        }
        validatePanelStatus();
    }

    private void checkDelegationPathDocument() {

        delegationText.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                changeDelegationPathBorderColor(e);
            }

            public void removeUpdate(DocumentEvent e) {
                changeDelegationPathBorderColor(e);
            }

            public void changedUpdate(DocumentEvent e) {
                changeDelegationPathBorderColor(e);
            }

            private void changeDelegationPathBorderColor(DocumentEvent e) {
                String strDelegationPath = delegationText.getText();
                try {
                    File typedFile = new File(strDelegationPath);
                    if (!strDelegationPath.equals("")
                            && filterAssertion.accept(typedFile)) {

                        delegationText.setBorder(
                                javax.swing.BorderFactory.createLineBorder(
                                new java.awt.Color(0, 0, 0)));

                        currentCapabilityDir = typedFile;

                    } else {

                        delegationText.setBorder(
                                javax.swing.BorderFactory.createLineBorder(
                                new java.awt.Color(255, 0, 0)));
                    }
                } catch (Exception ex) {
                }
            }
        });
    }

    public void setAccessTypeRadioListener(ActionListener listener){
        accessPolicyRadio.addActionListener(listener);
        accessCapabilityRadio.addActionListener(listener);
    }

    public void validatePanelStatus() {
        delegationPanel.setVisible(accessCapabilityRadio.isSelected());
    }
}
